/**
 * 
 */
package org.xelnaga.defiler.business.contentlet;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jboss.seam.wiki.core.nestedset.query.NestedSetNodeWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.xelnaga.defiler.business.FolderAPI;
import org.xelnaga.defiler.domain.FolderModel;
import org.xelnaga.defiler.service.FolderService;

/**
 * @author qijb
 *
 */
@Component
public class FolderAPIImpl implements FolderAPI {
	protected static final Logger log = Logger.getLogger(FolderAPIImpl.class);
	@Autowired
	private FolderService folderService;
	/* (non-Javadoc)
	 * @see org.xelnaga.defiler.business.FolderAPI#getTopNavigation()
	 */
	@Override
	public List<FolderModel> getTopNavigation(Long siteInode) {
		return this.folderService.getListTopNavBySiteInode(siteInode);
	}
	@Override
	public NestedSetNodeWrapper<FolderModel> getNavigation(Long siteInode,
			Integer startLevel, Long depth, String currentPath) {
		FolderModel folder = folderService.getBySiteAndPath(siteInode, currentPath);
		if(null != folder) {
			NestedSetNodeWrapper<FolderModel> result = folderService.getFolderTree(folder.copy(), depth, 0l, false);
			return result;
		}
		return null;
	}
	@Override
	public List<FolderModel> getAncestorListByInode(Long inode) {
		return folderService.getAncestorListByInode(inode, true);
	}
	
	@Override
	public List<FolderModel> getSiblingListByInode(Long inode) {
		return folderService.getSiblingListByInode(inode, true);
	}
	@Override
	public FolderModel getByInode(Long inode) {
		return folderService.getByPrimaryKey(inode);
	}
	@Override
	public List<FolderModel> getChildrenListByInode(Long inode) {
		return folderService.getListByParentInode(inode, true);
	}
	@Override
	public FolderModel getCurrentOrParentByInode(Long inode) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public boolean hasChildren(FolderModel folder) {
		return folder.getNodeInfo().getNsRight().longValue() - folder.getNodeInfo().getNsLeft().longValue() > 1;
//		return folderService.getListByParentInode(inode, true).size() > 0;
	}
	
	@Override
	public List<FolderModel> getRecommandListByInode(Long inode, Integer count) {
		FolderModel root = folderService.getByPrimaryKey(inode);
		if(null == root) {
			return new ArrayList<FolderModel>();
		}
		return folderService.getSortedListByParentInode(root, count, true);
	}
	
}
